﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using iTunesUtil.Properties;
using iTunesExport.Parser;

namespace iTunesUtil
{
    public partial class OptionsDlg : Form
    {
        static Settings set = Settings.Default;
        public OptionsDlg()
        {
            InitializeComponent();
            InitOutputTab();
            InitFileTab();                       
        }
        private void OnOK(object sender, EventArgs e)
        {
            SaveOutputTab();
            SaveFileTab();
            set.Save();
        }      

        #region File format tab

        private void InitFileTab()
        {
            checkBoxNumerate.Checked = set.FileNumerate;
            switch (set.FileFormat)
            {
                case (int)ExportFileFormatter.FileFormat.OriginalFileName:
                    radioButton1.Checked = true;
                    break;
                case (int)ExportFileFormatter.FileFormat.GenreArtistAlbumTrack:
                    radioButton2.Checked = true;
                    break;
                case (int)ExportFileFormatter.FileFormat.ArtistAlbumTrack:
                    radioButton3.Checked = true;
                    break;
                case (int)ExportFileFormatter.FileFormat.AlbumTrack:
                    radioButton4.Checked = true;
                    break;
                case (int)ExportFileFormatter.FileFormat.Track:
                    radioButton5.Checked = true;
                    break;
                default:
                    radioButton1.Checked = true;
                    break;
            }
            RefreshDemoTrack();
        }
        private void SaveFileTab()
        {
            checkBoxNumerate.Checked = set.FileNumerate;
            if (radioButton1.Checked)
                set.FileFormat = (int)ExportFileFormatter.FileFormat.OriginalFileName;
            else if (radioButton2.Checked)
                set.FileFormat = (int)ExportFileFormatter.FileFormat.GenreArtistAlbumTrack;
            else if (radioButton3.Checked)
                set.FileFormat = (int)ExportFileFormatter.FileFormat.ArtistAlbumTrack;
            else if (radioButton4.Checked)
                set.FileFormat = (int)ExportFileFormatter.FileFormat.AlbumTrack;
            else if (radioButton5.Checked)
                set.FileFormat = (int)ExportFileFormatter.FileFormat.Track;
        }

        
        private void RefreshDemoTrack()
        {
            ExportFileFormatter eff = new ExportFileFormatter("Z:\\Export\\");
            textBox1.Text = eff.Generate(Track.CreateDemoTrack(), 17);
        }            
        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            set.FileFormat = (int)ExportFileFormatter.FileFormat.Track;
            RefreshDemoTrack();
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            set.FileFormat = (int)ExportFileFormatter.FileFormat.AlbumTrack;            
            RefreshDemoTrack();
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            set.FileFormat = (int)ExportFileFormatter.FileFormat.ArtistAlbumTrack;
            RefreshDemoTrack();
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            set.FileFormat = (int)ExportFileFormatter.FileFormat.GenreArtistAlbumTrack;
            RefreshDemoTrack();
        }
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            set.FileFormat = (int)ExportFileFormatter.FileFormat.OriginalFileName;
            RefreshDemoTrack();
        }
        private void checkBoxNumerate_CheckedChanged(object sender, EventArgs e)
        {
            set.FileNumerate = checkBoxNumerate.Checked;
            RefreshDemoTrack();
        }
        #endregion

        #region Output tab

        private void InitOutputTab()
        {
            textBoxSelDir.Text = set.ExportDir;
            checkBoxDeleteDirContent.Checked = set.ExportDirClear;

            checkBoxMaxNoOfFiles.Checked = set.ExportNoFilesUse;
            numericUpDownMaxNoFiles.Value = set.ExportNoFiles;
            checkBoxAllfiles_CheckedChanged(this, new EventArgs());


            checkBoxMaxDirSize.Checked = set.ExportMaxDirSizeUse;
            numericUpDownMaxDirSize.Value = set.ExportMaxDirSize;
            checkBoxUnlimited_CheckedChanged(this, new EventArgs());
        }
        private void SaveOutputTab()
        {
            set.ExportDir = textBoxSelDir.Text;
            set.ExportDirClear = checkBoxDeleteDirContent.Checked;

            set.ExportNoFilesUse = checkBoxMaxNoOfFiles.Checked;
            set.ExportNoFiles = (int)numericUpDownMaxNoFiles.Value;

            set.ExportMaxDirSizeUse = checkBoxMaxDirSize.Checked;
            set.ExportMaxDirSize = (int)numericUpDownMaxDirSize.Value;
        }

        static public int MaxNoFiles
        {
            get { return (set.ExportNoFilesUse) ? set.ExportNoFiles : -1; }
        }
        static public long MaxDirSizeMB
        {
            get { return (set.ExportMaxDirSizeUse) ? set.ExportMaxDirSize : -1; }
        }
        private void OnSelectDir(object sender, EventArgs e)
        {
            Program.BrowseForFolder(textBoxSelDir, "Select Output Directory", true);
        }

        private void checkBoxUnlimited_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownMaxDirSize.Enabled = checkBoxMaxDirSize.Checked;         
        }

        private void checkBoxAllfiles_CheckedChanged(object sender, EventArgs e)
        {
            numericUpDownMaxNoFiles.Enabled = checkBoxMaxNoOfFiles.Checked;                        
        }
        #endregion
    }
}
